package com.meiyu.live.tv.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.meiyu.live.R;
import com.meiyu.live.popj.Assort;
import com.meiyu.live.ui.columnfollow.ColumnFollowView;
import com.meiyu.live.util.DisplayUtils;
import com.meiyu.live.widget.XCFlowLayout;

import java.util.ArrayList;
import java.util.List;


/**
 * 开始直播，类别选择对话框
 */
public class AssortSelectDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private ViewGroup.MarginLayoutParams lp;

    private View btn_ok;
    private XCFlowLayout flowlayout_assort;

    private List<Assort> assortList = new ArrayList<>();
    private Assort selectedBean;
    private AssortSelectedListener listener;

    public interface AssortSelectedListener {
        void onAssortSelected(Assort assort);
    }

    public AssortSelectDialog(Context context) {
        super(context, R.style.dialog_custom);
        init(context);
    }

    public AssortSelectDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        setContentView(R.layout.dialog_assortselect);
        setCanceledOnTouchOutside(false);
        lp = new ViewGroup.MarginLayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = DisplayUtils.dpToPixel(context, 5);
        lp.rightMargin = lp.leftMargin;
        lp.topMargin = lp.rightMargin;
        lp.bottomMargin = lp.topMargin;

        btn_ok = findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(this);
        flowlayout_assort = (XCFlowLayout) findViewById(R.id.flowlayout_assort);
    }

    public void setListener(AssortSelectedListener listener) {
        this.listener = listener;
    }

    public void setData(List<Assort> list) {
        if (list != null && list.size() > 0) {
            this.assortList.clear();
            this.assortList.addAll(list);
            update();
        }
    }

    private void update() {
        flowlayout_assort.removeAllViews();
        for (Assort assort : assortList) {
            final ColumnFollowView followView = new ColumnFollowView(getContext());
            followView.setOnClickListener(onClickListener);
            followView.setData(assort);
            followView.update();
            flowlayout_assort.addView(followView, lp);
        }
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            for (int i = 0; i < flowlayout_assort.getChildCount(); i++) {
                ColumnFollowView followView = (ColumnFollowView) flowlayout_assort.getChildAt(i);
                if (view == followView) {
                    followView.getData().isSelected = !followView.getData().isSelected;
                    if (followView.getData().isSelected) {
                        selectedBean = assortList.get(i);
                    } else {
                        selectedBean = null;
                    }
                } else {
                    followView.getData().isSelected = false;
                }
                followView.update();
            }
        }
    };

    @Override
    public void onClick(View view) {
        if (view == btn_ok) {
            if (listener != null) {
                listener.onAssortSelected(selectedBean);
            }
            dismiss();
        }
    }
}
